library(flextable)

Attaching package: ‘flextable’

The following object is masked from ‘package:purrr’:

    compose
read_icd_mapping <- function(model){
  path <- str_glue("data/chatgpt_embeddings/text-embedding-3-small/{model}_diagnoses_chatgpt_embeddings_to_ICD.csv")
  path <- here(path)
  read_csv(path)
}
df_gpt3.5_mapping <- read_icd_mapping("gpt-3.5-turbo-1106")
df_gpt4.0_mapping <- read_icd_mapping("gpt-4-turbo-preview")
df_claude3_haiku_t1.0_mapping <- read_icd_mapping("claude-3-haiku-20240307_t1-0")
df_claude3_opus_t1.0_mapping <- read_icd_mapping("claude-3-opus-20240229_t1-0")
df_gemini1.0_pro_t1.0_mapping <- read_icd_mapping("gemini-1.0-pro-002_t1-0")
df_gemini1.5_pro_t1.0_mapping <- read_icd_mapping("gemini-1.5-pro-001_t1-0")
embedding_mapping_long <- combine_data_frames(
df_gpt3.5_mapping,
df_gpt4.0_mapping,
df_claude3_haiku_t1.0_mapping,
df_claude3_opus_t1.0_mapping ,
df_gemini1.0_pro_t1.0_mapping,
df_gemini1.5_pro_t1.0_mapping
) %>% 
  unite(ICD_diagnosis, code, diagnosis.y, sep = " ") %>% 
  select(original_diagnosis=diagnosis.x, ICD_diagnosis) %>% 
  distinct()

embedding_mapping_wide <- embedding_mapping_long %>% 
  nest(.by = "ICD_diagnosis") %>% 
  mutate(n = map_dbl(data, nrow)) %>% 
  mutate(data = map_chr(data, ~paste(pull(.),collapse = "  •  ")))
embedding_mapping_wide
embedding_mapping_wide %>% 
  select(-n) %>% 
  filter(grepl("D47.02 Systemic mastocytosis", ICD_diagnosis)) %>% 
  rename(`ICD diagnosis`=ICD_diagnosis, `Original LLM output`=data) %>% 
  flextable() %>% 
  # width(width = 4) %>% 
  # autofit(part="body") %>% 
  width(j=2,width=5) %>% 
  width(j=1,width=2) %>% 
  fontsize(size = 9) %>% 
  fontsize(size = 10, part = "header") %>% 
  padding(padding = 0) %>% 
  # align(j = 2:3, align = "center", part = "all") %>% 
  set_table_properties(opts_pdf = list(arraystretch = 1.25)) %>% 
  # {print(., preview = "pdf");.}
  {print(., preview = "docx");.}

ICD diagnosis

Original LLM output

D47.02 Systemic mastocytosis

angioedema associated with systemic mastocytosis • gastrointestinal mastocytosis • lupus mastocytosis • mastocytosis • mastocytosis (cutaneous and systemic) • systemic basophilic granulocytosis • systemic mastocytosis • elderly onset systemic mastocytosis • mast cell disorders (such as systemic mastocytosis) • mastocytosis (including cutaneous and systemic types) • mastocytosis (including cutaneous and systemic) • mastocytosis (including systemic mastocytosis and cutaneous mastocytosis) • mastocytosis (including systemic mastocytosis) • mastocytosis (systemic mastocytosis) • mastocytosis (systemic or cutaneous) • mastocytosis (systemic) • systemic mast cell disease • bullous mastocytosis • indolent systemic mastocytosis • fungal pneumonia with systemic mastocytosis • mast cell disorders (e.g., systemic mastocytosis) • mastocytosis with eosinophilia and otitis • mastocytosis with systemic involvement • mastocytosis with systemic symptoms • acute mastocytosis • generalized mastocytosis • genetic syndromes that involve mastocytosis, such as gorham-stouts disease or mccune-albright syndrome • hereditary mastocytosis • hypothetical, systemic mastocytosis (icd code d820). see 42820 • masocytosis • masto mastocytosis • mastocytosis (mc) • mastocytosis and systemic anaphylaxis, idiopathic • mastocytosis with an associated hematological neoplasm • mastocytosis with an associated solid tumor • mastocytosis with angioedema and syncope • mastocytosis with angioedema, syncope, rhinorrhea, and incontinence • mastocytosis with angioedema, vomiting, sneezing, and rhinorrhea • mastocytosis with associated flushing, vomiting, and laryngeal edema • mastocytosis with dysautonomia, hypersomnia and angioedema • mastocytosis with flushing • mastocytosis with flushing, hypotension, and conjunctival injection • mastocytosis with gastrointestinal involvement • mastocytosis with hypotension and angioedema • mastocytosis with syncope • mastocytosis with syncope and flushing • mastocytosis with urticaria, angioedema, and hypotension • mastocytosis, • mastocytosis, indolent systemic • mastocytosis, systemic • mastocytosis, systemic, with pruritis, vomiting, and diarrhea • mastocytosis: systemic (urticaria pigmentosa and extracutaneous mastocytosis, including mast cell leukemia) • systemic mastocytosis (sm) • systemic mastocytosis with leukocytosis and angiomata • justification: can cause similar gastrointestinal symptoms, but nasal congestion would be unusual., systemic mastocytosis • justification: systemic mastocytosis is a rare disorder characterized by the accumulation of mast cells in various tissues, leading to a variety of symptoms, including abdominal cramps, diarrhea, hives, angioedema, and in some cases, incontinence., anaphylaxis • mastocytosis (a rare disorder) • mastocytosis (consider if recurrent) • mastocytosis (e.g., systemic mastocytosis) • mastocytosis (e.g., systemic mastocytosis, cutaneous mastocytosis) • mastocytosis (rare) • mastocytosis (rare, but can cause all these symptoms) • mastocytosis (systemic mast cell activation disorder) • mastocytosis (systemic mastocytosis, cutaneous mastocytosis)

LS0tCnRpdGxlOiAiSUNEIE1hcHBpbmcgVGFibGUiCm91dHB1dDogaHRtbF9ub3RlYm9vawotLS0KCmBgYHtyfQpsaWJyYXJ5KGhlcmUpCmxpYnJhcnkodGlkeXZlcnNlKQpsaWJyYXJ5KGZsZXh0YWJsZSkKc291cmNlKGhlcmUoInV0aWxzL2dlbmVyYWwuUiIpKQpgYGAKCmBgYHtyLCBtZXNzYWdlID0gRn0KcmVhZF9pY2RfbWFwcGluZyA8LSBmdW5jdGlvbihtb2RlbCl7CiAgcGF0aCA8LSBzdHJfZ2x1ZSgiZGF0YS9jaGF0Z3B0X2VtYmVkZGluZ3MvdGV4dC1lbWJlZGRpbmctMy1zbWFsbC97bW9kZWx9X2RpYWdub3Nlc19jaGF0Z3B0X2VtYmVkZGluZ3NfdG9fSUNELmNzdiIpCiAgcGF0aCA8LSBoZXJlKHBhdGgpCiAgcmVhZF9jc3YocGF0aCkKfQpkZl9ncHQzLjVfbWFwcGluZyA8LSByZWFkX2ljZF9tYXBwaW5nKCJncHQtMy41LXR1cmJvLTExMDYiKQpkZl9ncHQ0LjBfbWFwcGluZyA8LSByZWFkX2ljZF9tYXBwaW5nKCJncHQtNC10dXJiby1wcmV2aWV3IikKZGZfY2xhdWRlM19oYWlrdV90MS4wX21hcHBpbmcgPC0gcmVhZF9pY2RfbWFwcGluZygiY2xhdWRlLTM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